package cn.xo68.boot.auth.server.domain.dto;

import cn.xo68.boot.auth.server.domain.OauthUser;
import cn.xo68.boot.auth.server.domain.OauthUserRole;
import cn.xo68.core.date.DateTime;
import cn.xo68.core.util.UUIDUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户传输模型
 * @author  wuxie
 * @date  2018-9-21
 */
public class OauthUserDto implements Serializable {

    private String userId;
    /**
     * 登录帐户
     */
    private String account;
    private String userPass;
    /**
     * 用户真名或昵称
     */
    private String userName;
    /**
     * 用户性别：m男，w女
     */
    private String sex;
    /**
     * 用户类型，0管理员，1普通管理
     */
    private Integer userType;
    private Date birthday;
    private String phone;
    private String email;

    private List<String> roleIds;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getUserPass() {
        return userPass;
    }

    public void setUserPass(String userPass) {
        this.userPass = userPass;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<String> getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(List<String> roleIds) {
        this.roleIds = roleIds;
    }

    public OauthUser buildUser(String operatorName, String operatorId){
        OauthUser user=new OauthUser();
        BeanUtils.copyProperties(this, user, "roleDtos");
        return user;
    }

    public List<OauthUserRole> buildRoleList(String userId, String operatorName, String operatorId){
        List<OauthUserRole> roles=new ArrayList<>();
        for (String roleId: this.getRoleIds()) {
            OauthUserRole role=new OauthUserRole();
            role.setSerialId(UUIDUtils.createUUId());
            role.setUserId(userId);
            role.setRoleId(roleId);

            role.setOperatorId(operatorId);
            role.setOperatorName(operatorName);

            role.setCreateTime(DateTime.Now().getDate());
            role.setUpdateTime(DateTime.Now().getDate());

            roles.add(role);
        }
        return roles;
    }
}
